<?
session_start();
include_once (dirname(__FILE__) . '/libs/database/db.php');
include_once ("config.php");
require_once ("libs/JSON.php");

// action for project editor
$action = $_POST['action'];
if ($action == "add")
	add_new_project();
if ($action == "del")
	del_project();
if ($action == "update")
	update_project();
if ($action == "login")
	login();
if ($action == "logout")
	logout();

// action for category editor
$category_action = $_POST['category_action'];	
if ($category_action == "update_or_add")
{
	if ($_POST['cat_inner_id'] == "-1")
		add_category();
	else
		update_category();
}
if ($category_action == "delete")
	delete_category();
	
	
function add_category(){
	// $id = $_POST['cat_inner_id'];
	$cat_prj_id = $_POST['cat_prj_id'];
	$cat_color = $_POST['cat_color'];
	$cat_icon = $_POST['cat_icon'];
	$cat_tape_image = $_POST['cat_tape_image'];
	$cat_title_link = $_POST['cat_title_link'];
	
	$sql = "INSERT INTO " . TB_CONFIG .
	" (PRJ_ID, COLOR, ICON, TAPE_IMAGE, TITLE_LINK) VALUES('$cat_prj_id'," .
	" '$cat_color'," .
	" '$cat_icon'," .
	" '$cat_tape_image'," .
	" '$cat_title_link')";

	mysql_query($sql);
}
	
function update_category(){
	$id = $_POST['cat_inner_id'];
	$cat_prj_id = $_POST['cat_prj_id'];
	$cat_color = $_POST['cat_color'];
	$cat_icon = $_POST['cat_icon'];
	$cat_tape_image = $_POST['cat_tape_image'];
	$cat_title_link = $_POST['cat_title_link'];
	
	$sql = "UPDATE " . TB_CONFIG .
	" SET PRJ_ID='$cat_prj_id'," .
	" COLOR='$cat_color'," .
	" ICON='$cat_icon'," .
	" TAPE_IMAGE='$cat_tape_image'," .
	" TITLE_LINK='$cat_title_link'" .
	" WHERE ID=$id";

	mysql_query($sql);
}

function delete_category(){
	$id = $_POST['cat_inner_id'];
	$sql = "DELETE FROM ". TB_CONFIG. " WHERE ID=$id";
	
	mysql_query($sql);
}

function add_new_project() {
	$start_time = date("Y-m-d H:i:s");
	$sql = "INSERT INTO " . TB_EVENT . "(TITLE, START) VALUES('Untitled Project', '$start_time')";

	mysql_query($sql);
}

function del_project() {
	$pid = $_POST['pid'];
	$sql = "DELETE FROM " . TB_EVENT . " WHERE ID=$pid";

	mysql_query($sql);
}

function update_project() {
	$id = $_POST['id'];
	$title = htmlspecialchars($_POST['prj_title']);
	$start_time = $_POST['start_time'];
	$end_time = $_POST['end_time'];
	if (trim($end_time) == "")
		$end_time = "NULL";
	else
		$end_time = "'$end_time'";
	$image = $_POST['image'];
	$color = $_POST['color'];
	$icon = $_POST['icon'];
	$complete = $_POST['complete'];
	$priority = $_POST['priority'];
	$is_duration = $_POST['is_duration'];
	$description = htmlspecialchars($_POST['description']);
	$owner = htmlspecialchars($_POST['owner']);
	$tape_image = $_POST['tape_image'];
	$page_url = $_POST['page_url'];
	$title_link = $_POST['title_link'];
	$prj_id = $_POST['prj_id'];

	$sql = "UPDATE " . TB_EVENT .
	" SET TITLE='$title'," .
	" TITLE='$title'," .
	" START='$start_time'," .
	" END=$end_time," .
	" IMAGE='$image'," .
	" COLOR='$color'," .
	" ICON='$icon'," .
	" COMPLETE='$complete'," .
	" PRIORITY='$priority'," .
	" IS_DURATION='$is_duration'," .
	" DESCRIPTION='$description'," .
	" OWNER='$owner'," .
	" TAPE_IMAGE='$tape_image'," .
	" PAGE_URL='$page_url'," .
	" TITLE_LINK='$title_link'," .
	" PRJ_ID='$prj_id'" .
	" WHERE ID=$id";

	mysql_query($sql);
}

function login() {
	$username = $_POST['username'];
	$password = $_POST['password'];

	if ($username == ADMIN && $password == PASSWD)
		$_SESSION['is_login'] = "true";
	else
		$_SESSION['is_login'] = "";

	echo $_SESSION['is_login'];
}

function logout() {
	$_SESSION['is_login'] = "";
}
?>
